package mysql;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.InputStream;
import java.sql.*;

public class JdbcUtil {
    private final String url;
    private final String username;
    private final String password;
    private Connection conn;
    private PreparedStatement pstmt;



    public JdbcUtil() {
        ObjectMapper mapper = new ObjectMapper();
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("db-config.json");
        ) {
            DBConfig config = mapper.readValue(inputStream, DBConfig.class);

            this.url = config.getUrl();
            this.username = config.getUsername();
            this.password = config.getPassword();
            // 加载JDBC驱动
            Class.forName(config.getDriver());
        } catch (Exception e) {
            throw new RuntimeException("加载MYSQL驱动错误", e);
        }
    }

    public Connection connectMySQL() throws SQLException {
        if (conn == null || conn.isClosed()) {
            conn = DriverManager.getConnection(url, username, password);
        }
        return conn;
    }

    public PreparedStatement createpstmt(String sql) throws SQLException {
        pstmt=conn.prepareStatement(sql);
        return pstmt;
    }

    public boolean executeSQL(String sql) throws SQLException {
        try (PreparedStatement pstmt =createpstmt(sql)) {
            return pstmt.executeUpdate() > 0;
        }
    }

    public ResultSet querySQL(String sql) throws SQLException {
        pstmt = createpstmt(sql);
        return pstmt.executeQuery();
    }

    public void disconnectMySQL() {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
            if(pstmt != null && !pstmt.isClosed()) {
                pstmt.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException("Close error", e);
        }
    }

    public void Insert(String tablename, String values_name, String values) {
        PreparedStatement ps;

        if (this.checkNotNull(tablename)&&this.checkNotNull(values_name)&&this.checkNotNull(values)) {
            try (Connection con =this.connectMySQL()){
                ps = this.createpstmt("INSERT INTO " + tablename + "(" + values_name + ") VALUES" + values);
                ps.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void Delete(String sql) throws SQLException {
        Connection conn = this.connectMySQL();
        Statement stmt = conn.createStatement();
        stmt.executeUpdate(sql);
    }

    /**
     * @param check_name :判断的字段
     * @return 如果check_name不为空返回返回 true
     *          否则返回 false
     */
    public boolean checkNotNull(String check_name) {
        // 如果check_name不为null并且不为空，且不等于"null"返回true
        return check_name != null && !check_name.isEmpty() && !"null".equals(check_name);
    }



    // Database configuration inner class
    private static class DBConfig {
        private String url;
        private String username;
        private String password;
        private String driver;

        // Getters and setters
        public String getUrl() { return url; }
        public void setUrl(String url) { this.url = url; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
        public String getDriver() { return driver; }
        public void setDriver(String driver) { this.driver = driver; }
    }

}

